(function($) {

	$.SJR = function(){};

	$.SJR.JSONENGINE = "1.0.0"

	$.SJR.option = {
		"type" : "",
		"url"  : "",
		"data" : null,
		"success" : null,
		"error"   : null
	};
	var success = function(response, status){
		var _success = function(response, status){
			if($.SJR.option.type=='DELETE'){
				var o = {};
				for(var i in $.SJR.option.data)
					o[i] = '';
				$.SJR.thisObject.values(o);
			}
			else{
				$.SJR.thisObject.values(response);
			}
		};
		var fn_success = function(response, status){
			if($.SJR.option.type=='DELETE'){
			}
			else{
				var t = $.SJR.thisObject;
				t.empty();
				for(var i in response){
					var div = $('<div/>');
//					for(var ii in response[i]){
//						var s = $('<span/>');
//						s.append(ii+':'+response[i][ii]);
//						div.append(s);
//					}
					div.append(JSON.stringify(response[i]))
					div.click(function(){
						$("#message").values(JSON.parse($(this).text()));
					});
					t.append((div));
				}
			}
		};
		var _exec = ($.SJR.isFn)? fn_success:_success;
		_exec(response, status);
	};
	var error = function(xhr, status, thrown){
		window.alert('error');
	};

	$.SJR.clear = function(){
		$.SJR.option.data = {};
		$.SJR.option.success = success;
		$.SJR.option.error = error;
	};
	$.SJR.clear();

	$.SJR.ajax = function(option) {
		if(option)
			$.SJR.option = option;
		$.ajax($.SJR.option);
	};

	$.SJR.ajaxbase = function(obj, type, config, success, error){
		var _success = function(response, status){
			$.SJR.thisObject.success(response, status);
			if(type!=='DELETE')
				$.SJR.thisObject.values(response);
			else{
				var o = {};
				for(var i in $.SJR.option.data)
					o[i] = '';
				$.SJR.thisObject.values(o);
			}
		};
		var _error = function(xhr, status, thrown){
			$.SJR.thisObject.error(xhr, status, thrown);
		};
		$.SJR.clear();
		$.SJR.thisObject = obj;
		$.SJR.configObject = config;
		if(type)
			$.SJR.option.type = type;
		if(config)
			if($.isPlainObject(config)){
				if(config.url){
					$.SJR.option.url = config.url;
				}
				if(config.data){
					$.SJR.option.data = config.data;
				}
			}else{
				if($(config).get(0).tagName=='FORM'){
					if(type!=='DELETE'){
						$.SJR.option.url = $(config).attr('action');
						$.SJR.option.data = $(config).values();
						if(type=='POST'){
							delete $.SJR.option.data._docId;
						}
					}
					else{
						var url = $(config).attr('action');
						var data = $(config).values();
						$.SJR.option.url = url + '/' + data._docId;
						$.SJR.option.data = data;
					}
				}
			}
		if(success){
			obj.success = success;
			$.SJR.option.success = _success;
		}
		if(error){
			obj.error = error;
			$.SJR.option.error = _error;
		}
		$.ajax($.SJR.option);
	};

	var isDocId = function(o){
		var data = $(o).values();
		if(!data._docId){
			window.alert('no _docId');
			return 0;
		}
		if(jQuery.trim(data._docId)==''){
			window.alert('no _docId');
			return 0;
		}
		return 1;
	};

	$.extend({
		_GET : function(config, success, error) {
			$.SJR.isFn=0;
			$.SJR.ajaxbase(config, "GET", config, success, error);
		},
		_POST : function(config, success, error) {
			$.SJR.isFn=0;
			$.SJR.ajaxbase(config, "POST", config, success, error);
		},
		_PUT : function(config, success, error) {
			if(!isDocId(config)) return;
			$.SJR.isFn=0;
			$.SJR.ajaxbase(config, "PUT", config, success, error);
		},
		_DELETE : function(config, success, error) {
			if(!isDocId(config)) return;
			$.SJR.isFn=0;
			$.SJR.ajaxbase(config, "DELETE", config, success, error);
		}
	});

	$.fn.extend({
		_GET : function(config, success, error) {
			$.SJR.isFn=1;
			$.SJR.ajaxbase(this, "GET", config, success, error);
			return this;
		},
		_POST : function(config, success, error) {
			$.SJR.isFn=1;
			$.SJR.ajaxbase(this, "POST", config, success, error);
			return this;
		},
		_PUT : function(config, success, error) {
			if(!isDocId(config)) return;
			$.SJR.isFn=1;
			$.SJR.ajaxbase(this, "PUT", config, success, error);
			return this;
		},
		_DELETE : function(config, success, error) {
			if(!isDocId(config)) return;
			$.SJR.isFn=1;
			$.SJR.ajaxbase(this, "DELETE", config, success, error);
			return this;
		}
	});

})(jQuery);

